package com.utn.tp.integrador.adaptadores;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;

import com.utn.tp.integrador.R;
import com.utn.tp.integrador.entidades.Contacto;

public class MainAndroidContactsAdapter extends BaseAdapter implements
		Filterable {
	private String contactFilter;
	private boolean contactFilterApplied;
	private ArrayList<Contacto> contacts;
	private LayoutInflater inflater;
	private ContactFilter filter;

	public MainAndroidContactsAdapter(Context context) {
		contacts = new ArrayList<Contacto>();
		inflater = LayoutInflater.from(context);
	}

	public void addContact(Contacto contact) {
		if (contact != null) {
			contacts.add(contact);
		}
	}

	public void clear() {
		contacts.clear();
	}

	@Override
	public int getCount() {
		return contacts.size();
	}

	@Override
	public Filter getFilter() {
		if (filter == null) {
			filter = new ContactFilter();
		}
		return filter;
	}

	@Override
	public Object getItem(int position) {
		return contacts.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {
		Holder holder;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.lista_contactos_item, null);
			holder = new Holder();
			holder.txtName = (TextView) convertView
					.findViewById(R.id.txtnombre);
			holder.imagenContacto = (ImageView) convertView
					.findViewById(R.id.imagen_contacto);
			convertView.setTag(holder);
		} else {
			holder = (Holder) convertView.getTag();
		}

		Contacto contact = (Contacto) getItem(position);
		holder.txtName.setText(contact.getName());
		if (contact.getImagenContacto() != null) {
			holder.imagenContacto.setImageURI(Uri.parse(contact
					.getImagenContacto()));
		} else {
			holder.imagenContacto.setImageResource(R.drawable.ic_launcher);
		}
		return convertView;
	}

	public void sort(Comparator<Contacto> comparator) {
		Collections.sort(contacts, comparator);
	}

	class Holder {
		private TextView txtName;
		private ImageView imagenContacto;
	}

	class ContactFilter extends Filter {

		@Override
		protected FilterResults performFiltering(CharSequence constraint) {
			String filter = constraint.toString().toLowerCase().trim();
			contactFilter = filter;
			return null;
		}

		@Override
		protected void publishResults(CharSequence constraint,
				FilterResults results) {
			publishFilterApplied();
		}
	}

	private void publishFilterApplied() {
		if (contactFilter == null || contactFilter.length() == 0) {
			setContactFilterApplied(false);
		} else {
			setContactFilterApplied(true);
		}
		// loadDialogData();
	}

	public boolean isContactFilterApplied() {
		return contactFilterApplied;
	}

	public void setContactFilterApplied(boolean contactFilterApplied) {
		this.contactFilterApplied = contactFilterApplied;
	}
}
